package com.power.using.mydaggerp.presenter.book.tabs;

import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.power.using.mydaggerp.contract.book.tabs.BookCustomContract;
import com.power.using.mydaggerp.model.bean.douban.book.BookItemBean;
import com.power.using.mydaggerp.model.bean.douban.book.BookListBean;
import com.power.using.mydaggerp.model.book.tabs.BookCustomModel;
import com.power.using.mydaggerp.ui.activity.detial.BookDetailActivity;

import io.reactivex.functions.Consumer;

/**
 * Created by 聪明一只哈 on 2019/10/18.
 * Time: 2019/10/18  9:55
 */
public class BookCustomPresenter extends BookCustomContract.BookCustomPresenter{

    private int mStart;
    private int mCount = 30;
    private boolean isLoading;

    @NonNull
    public static BookCustomPresenter newInstance() {
        return new BookCustomPresenter();
    }


    @Override
    public void loadLatestBookList() {

        if(mIModel==null||mIView==null){
            return;
        }

        mStart=0;
        //一次加载20条
        mRxManage.register(mIModel.getBookListWithTag(mIView.getBookTags(),mStart,mCount)
                .subscribe(new Consumer<BookListBean>() {
                    @Override
                    public void accept(BookListBean bookListBean) throws Exception {
                        if(mIView==null){
                            return;
                        }


                        mStart+=mCount;
                        mIView.updateContentList(bookListBean.getBooks());

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if(mIView==null){
                            return;
                        }

                        if(mIView.isVisiable()){
                            mIView.showToast("Network error");
                        }

                        mIView.showNetworkError();

                    }
                }));

    }

    @Override
    public void loadMoreBookList() {
        if(!isLoading){
            isLoading=true;
            //一次20条
            mRxManage.register(mIModel.getBookListWithTag(mIView.getBookTags(),mStart,mCount)
            .subscribe(new Consumer<BookListBean>() {
                @Override
                public void accept(BookListBean bookListBean) throws Exception {
                    isLoading = false;
                    if (mIView == null)
                        return;

                    if(bookListBean!=null&&bookListBean.getBooks().size()>0){
                        mStart+=mCount;
                        mIView.updateContentList(bookListBean.getBooks());
                    }else{
                        mIView.showNoMoreData();
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    isLoading = false;
                    if (mIView != null) {
                        mIView.showLoadMoreError();
                    }
                }
            }));
        }

    }

    @Override
    public void onItemClick(int position, BookItemBean item, ImageView imageView) {
        BookDetailActivity.start(mIView.getBindActivity(), item, imageView);

    }

    @Override
    protected BookCustomContract.IBookCustomModel getModel() {
        return BookCustomModel.newInstance();
    }

    @Override
    public void onStart() {

    }
}
